<?php
namespace app\index\controller;

class Client extends Base {
    public $modelDb;
    public function _initialize()
    {
        parent::_initialize();
        $this->modelDb = model("Client");
    }
    //全部客户
    public function index() {
        $this->assign('where', $this->postData);
        $this->assign('title', '全部线索');
        return $this->fetch();
    }
    //我的客户-所有者
    public function personal() {
        $info = $this->modelDb->get_data(['item_id' => $this->postData['clientid']]);
        // $status = -1;
        // if ($info['kind'] == 'gouche') {
        // 	$status = model('MemberBouns')->where('userid', $this->uinfo['userid'])->where('clientid', $this->postData['clientid'])->value('status');
        // }
        // $this->assign('status', !is_null($status) ? $status : -1);
        $this->assign('info', $info);
        $this->assign('title', '');
        return $this->fetch();
    }
    //转交客户
    public function client_deliver() {
        $list = model('AdminUser')->list_subords($this->uinfo['userid']);
        $this->assign('list', $list);
        $this->assign('clientid', $this->postData['clientid']);
        $this->assign('title', '转交客户');
        return $this->fetch();
    }
    //数据统计
    public function data_stat() {
        $list = [];
        $otherwhere = [];
        if (isset($this->postData['addday'])) {
            $addday = $this->postData['addday'];
            if (isset($this->postData['nextmonth']) && $this->postData['nextmonth']) {
                $arr = explode('-', $this->postData['addday']);
                $nextmonth = $arr[1] + $this->postData['nextmonth'];
                if ($nextmonth == 0) {
                    $addday = ($arr[0] - 1) . '-12';
                } elseif ($nextmonth == 13) {
                    $addday = ($arr[0] + 1) . '-01';
                } else {
                    $addday = $arr[0] . '-' . str_pad($nextmonth, 2, 0, STR_PAD_LEFT);
                }
            }
            // $otherwhere = ['getdate'=> $addday];
        } else {
            $addday = date('Y-m', SYS_TIME);
            // $otherwhere = ['getdate' => $addday];
        }
        if (isset($this->postData['userid'])) {
            $otherwhere = ['brokerid' => $this->postData['userid']];
        }
        trace($otherwhere);
        // trace($this->uinfo['userid']);
        // trace($addday);
        // list($info, $list, $userids) = model('Statistics')->list_datas($addday, $this->uinfo['userid'], $this->uinfo['roleid'], '', isset($this->postData['userid']) ? $this->postData['userid'] : 0);
        // trace(count($list));
        $ulist = model('AdminUser')->where('dealerid', $this->uinfo['dealerid'])->field( 'userid, realname, avatar, dealerid')->order('userid asc')->select();
        $all_clients = model('Client')->where('dealerid', $this->uinfo['dealerid'])->where($otherwhere)->field( 'userid, name, carthumb, dealerid')->select();
        $suc_clients = model('Client')->where('dealerid', $this->uinfo['dealerid'])->where($otherwhere)->where('status', '2')->field( 'userid, name, carthumb, dealerid')->select();
        $info = [
            'clients'=>count($all_clients),
            'suc_clients' => count($suc_clients),
        ];
        $this->assign('ulist', $ulist); // 当前经销商下，所有的经纪人列表
        $this->assign('list', $all_clients); // 当前经销商下，所有的线索列表
        $this->assign('suc_list', $suc_clients); // 当前经销商下，所有的已成交的线索列表
        $this->assign('info', $info);
        $this->assign('addday', $addday);
        $this->assign('title', '数据统计');
        return $this->fetch();
    }
    //录入新客户
    public function clue_add() {
        $this->postData['formid'] = isset($this->postData['formid']) ? $this->postData['formid'] : '';
        $this->assign('formid', $this->postData['formid']);
        $this->assign('title', '录入新客户');
        return $this->fetch();
    }
    //编辑客户信息
    public function clue_edit() {
        $info = $this->modelDb->get_data(['item_id' => $this->postData['clientid']]);
        $this->assign('info', $info);
        $this->assign('title', '编辑客户信息');
        return $this->fetch();
    }
    //我的经纪人
    public function mysubord() {
        $list = model('AdminUser')->list_subords($this->uinfo['userid'], $this->uinfo['roleid'], $this->uinfo['puserid']);
        $this->assign('list', $list);
        $this->assign('title', '我的经纪人');
        return $this->fetch('myunder');
    }
    //添加我的经纪人
    public function add_mysubord() {
        $this->assign('title', '添加我的经纪人');
        return $this->fetch();
    }
    //编辑我的经纪人
    public function edit_mysubord() {
        $info = model('AdminUser')->get_data(['item_id' => $this->postData['userid']]);
        $this->assign('info', $info);
        $this->assign('title', '编辑我的经纪人');
        return $this->fetch();
    }
    //我的经纪人资料
    public function mysubord_detail() {
        $info = model('AdminUser')->get_data(['item_id' => $this->postData['userid']]);
        $this->assign('info', $info);
        $this->assign('title', $info['realname'] ? $info['realname'] : '我的经纪人资料');
        return $this->fetch();
    }
    //成交订单详情
    public function order()
    {
        $info = model('Client')->get_data(['item_id' => $this->postData['id']]);
        trace($info);
        $this->assign('info', $info);
        $this->assign('title', '成交订单详情');
        return $this->fetch();
    }
    //新建成交订单
    public function order_add() {
        $info = model('Client')->get_data(['item_id' => $this->postData['clientid']]);
        $this->assign('info', $info);
        $this->assign('title', '新建成交订单');
        return $this->fetch();
    }
    //修改订单
    public function order_edit() {
        $info = model('OrderClient')->get_data(['item_id' => $this->postData['id']]);
        $this->assign('info', $info);
        $this->assign('title', '修改订单');
        return $this->fetch();
    }
}
